package com.lottery.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BizException.class)
    @ResponseBody//自定义异常
    public ResultBody handleBizException(HttpServletRequest request, BizException e) {
        log.error("业务异常:{}", e.getMessage());
        return ResultBody.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody//空异常
    public ResultBody handleNullException(HttpServletRequest request, NullPointerException e) {
        log.error("空异常:{}", e.toString());
        return ResultBody.error(ErrorCode.ERROR_NULL);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBody handleException(HttpServletRequest request, Exception e) {
        log.error("未知异常:{}", e.toString());
        return ResultBody.error(ErrorCode.ERROR_INTERNAL);
    }
}
